package com.anxin.act.task.vo;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.anxin.act.task.entity.ViewRuTask;
import com.anxin.act.utils.ActUtils;
import com.anxin.sys.file.entity.FileBase;
//import io.swagger.annotations.Api;
//import io.swagger.annotations.ApiModelProperty;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskInfo;
import org.apache.tools.ant.types.FileList;

import java.util.*;

/**
 * 任务处理传值类
 *
 * @author: liuxiangyu
 * @date: 2020/3/30 18:52
 */
//@Api(tags = "正在运行的待办")
public class TaskVo {

    /**
     * ID
     */
    //@ApiModelProperty(value = "ID",required = true,position=1)
    private String id;

    /**
     * Activiti 待办ID
     */
    //@ApiModelProperty(value = "待办ID",required = true,position=1)
    private String taskId;

    //@ApiModelProperty(value = "待办节点标识",required = true,position=1)
    private String taskKey;

    //@ApiModelProperty(value = "待办类型",required = true,position=1)
    private String taskType;

    //@ApiModelProperty(value = "待办节点名称",required = false,position=1)
    private String taskName;

    //待办标题
    //@ApiModelProperty(value = "待办标题",required = true,position=2)
    private String title;

    //申请人ID
    //@ApiModelProperty(value = "申请人Id",required = true,position=3)
    private String applyUserId;

    //申请人登录名
    //@ApiModelProperty(value = "申请人登录名",required = true,position=4)
    private String applyUserName;

    //申请人显示名称
    //@ApiModelProperty(value = "申请人显示名称",required = true,position=5)
    private String applyNickName;

    /**
     * 审批人登录名
     */
    //@ApiModelProperty(value = "审批人登录名",required = true,position=5)
    private String assignee;

    //申请时间
    //@ApiModelProperty(value = "申请时间",required = true,position=6)
    private Date applyTime;

    //流程当前环节
    //@ApiModelProperty(value = "流程当前环节",required = true,position=7)
    private String current;

    //待办对应的流程名称
    //@ApiModelProperty(value = "待办对应的流程名称",required = true,position=8)
    private String processName;

    //流程定义的Key
    //@ApiModelProperty(value = "流程定义的Key",required = true,position=8)
    private String processDefKey;

    //流程定义的ID
    //@ApiModelProperty(value = "流程定义的ID",required = true,position=8)
    private String processDefId;

    //关联的业务数据ID
    //@ApiModelProperty(value = "关联的业务数据ID",required = true,position=9)
    private String dataId;

    //关联的业务数据表
    //@ApiModelProperty(value = "关联的业务数据表",required = true,position=10)
    private String dataTable;

    //流程实例ID
    //@ApiModelProperty(value = "流程实例ID",required = true,position=11)
    private String actId;

    // 待办表单组件路径
    //@ApiModelProperty(value = "待办表单组件路径",required = true,position=12)
    private String actFormPath;

    // 待办表单组件路径
    //@ApiModelProperty(value = "待办表单组件ID",required = true,position=12)
    private String actFormId;

    // 待办表单组件只读路径
    //@ApiModelProperty(value = "待办表单组件只读路径",required = true,position=12)
    private String actViewPath;

    /**
     * 是否加急 Y是  N否
     */
    //@ApiModelProperty(value = "是否加急",required = true,position=13)
    private String urgent;

    //@ApiModelProperty(value = "前加签和转签的备注",required = true,position=13)
    private String addTaskRemark;

    /**
     * 是否启用委托的判断
     */
    private boolean jumpEntrust;

    /**
     * 新节点名称（用于加签等生产新节点名称）
     */
    private String newTaskName;

    /**
     * 任务对象
     */
    private ViewRuTask viewRuTask;

    /**
     * 抄送用户
     */
    private JSONArray ccUser;

    /**
     * 加签用户
     */
    private JSONArray addUser;

    /**
     * 转签用户
     */
    private String entrustUser;

    /**
     * 下一步审批人
     */
    private String nextApproval;

    /**
     * 下一步审批人集合
     */
    private JSONArray nextApprovalArray;

    /**
     * 定制表单
     */
    private List<CustomFormVo> customFormVoList;


    /**
     * 待办提交类型
     * task:正常提交
     * addBefore:前加签提交
     * addAfter:后加签提交
     * reading:阅办提交
     * freeFlow:自由流提交
     * entrust:转签
     * rejected:驳回
     */
    private String completeType;
    /**
     * 驳回节点
     */
    private String rejectedNode;
    /**
     * 驳回后提交方式 normal:正常提交   return:直接返回当前节点
     */
    private String rejectedCompleteType;


    // 流程变量
    //@ApiModelProperty(value = "流程变量",required = true,position=13)
    private Map<String,Object> processVariables;

    /**
     * 审批意见配置的字段
     */
    private List<FormPropertyVo> formFieldList;

    /**
     * 审批时上传的附件ID集合
     */
    private JSONArray fileIdList;


    /**
     * 附件集合
     */
    private List<FileBase> fileList;


    /**
     * 流程配置属性-是否可以修改表单
     */
    private String actModifyFrom;


    /**
     * 是否为下一节点选择审批人
     */
    private String selectApproval;

    /**
     * 下一节点审批人
     */
    private JSONArray nextNodeApproval;

    /**
     * 关联流程
     */
    private JSONArray associatedProcess;

    /**
     * 是否可以查看审批记录
     */
    private String actApprovalRecord;

    /**
     * 是否可以查看流程图
     */
    private String actApprovalPng;

    /**
     * 审批时是否可以上传附件
     */
    private String uploadfile;

    /**
     * 待办任务对象
     */
    private Task task;

    /**
     * 需要待办页面打开的业务地址
     */
    private JSONArray businessPathArray;

    /**
     * 加签节点是否可以修改表单
     */
    private String addTaskModifyFrom;

    /**
     * 审批意见
     */
    private String supplementaryComments;

    /**
     * 审批意见id
     */
    private String supplementaryCommentsId;


    private String type;

    /**
     * activity节点描述
     */
    private String actTaskDocumentation;


    public TaskVo(){

    }

    /**
     * 初始化
     * @param task
     * @param viewRuTask
     */
    public TaskVo(TaskInfo task, ViewRuTask viewRuTask){
        this.init(task);
        this.id = viewRuTask.getId();
        this.taskId = viewRuTask.getTaskId();
        this.taskType = viewRuTask.getType();

    }


    /**
     * 将待办转成raskVo
     * @param task
     */
    public TaskVo(TaskInfo task){
        this.init(task);
        this.id = task.getId();
        this.taskId = task.getId();
        this.taskType = "actTask";
    }

    /**
     * 初始化
     * @param task
     */
    public void init(TaskInfo task){
        Map<String, Object> processVariables = task.getProcessVariables();
        this.processVariables = processVariables;
        this.taskKey = task.getTaskDefinitionKey();
        this.actId = task.getProcessInstanceId();
        this.processDefId = task.getProcessDefinitionId();
        this.title = processVariables.get("title")!=null?processVariables.get("title").toString():"";
        this.applyUserName = processVariables.get("applyUserName")!=null?processVariables.get("applyUserName").toString():"";
        this.applyNickName = processVariables.get("applyNickName")!=null?processVariables.get("applyNickName").toString():"";
        this.assignee = task.getAssignee();
        this.current = task.getName();
        this.applyTime = processVariables.get("applyTime") != null ? new Date(new Long(processVariables.get("applyTime").toString())):null;
        ProcessDefinition processDefinition = ActUtils.getProcessDefinition(task.getProcessDefinitionId());
        this.processName = processDefinition.getName();
        this.processDefKey = processDefinition.getKey();
        this.actFormPath = processVariables.get("actFormPath")!=null?processVariables.get("actFormPath").toString():"";
        this.actFormId = processVariables.get("actFormId")!=null?processVariables.get("actFormId").toString():"";
        this.actViewPath = processVariables.get("actViewPath")!=null?processVariables.get("actViewPath").toString():"";
        // 是否加急
        this.urgent = processVariables.get("urgent")!=null?processVariables.get("urgent").toString():"";

        // 关联流程
        this.associatedProcess = processVariables.get("associatedProcess")!=null? JSONArray.parseArray(processVariables.get("associatedProcess").toString()):new JSONArray();

        // 相关业务表单
        this.businessPathArray = processVariables.get("businessPathArray")!=null? JSONArray.parseArray(processVariables.get("businessPathArray").toString()):new JSONArray();
    }



    /**
     * 加载运行时相关数据
     * @return
     */
    public TaskVo initRuData(){
        Map<String, String> dataTableAndDataId = ActUtils.getDataTableAndDataId(this.actId);
        this.dataId = dataTableAndDataId.get("actDataId");
        this.dataTable = dataTableAndDataId.get("actDataTable");
        return this;
    }

    /**
     * 加载待办提交时候需要的数据
     * @return
     */
    public TaskVo initComplete(){

        return this;
    }




    /**
     * 获取审批意见录入的值
     * @return
     */
    public Map<String, Object> getOpinions() {
        Map<String, Object> resOpinions = new HashMap<>();

        if (formFieldList != null) {
            for (FormPropertyVo formPropertyVo : formFieldList) {
                resOpinions.put(formPropertyVo.getId(), formPropertyVo.getValue());
            }
        }
        return resOpinions;

    }

    /**
     * 获取审批表单填写的值
     * @param fieldName 字段名称
     * @return
     */
    public String getFormFieldValue(String fieldName){
        if (this.formFieldList != null){
            for (FormPropertyVo formPropertyVo : this.formFieldList) {
                if (fieldName.equals(formPropertyVo.getId())){
                    return formPropertyVo.getValue();
                }
            }
        }
        return null;
    }

    /**
     * 获取审批表单填写的值
     * @param fieldName 字段名称
     * @return
     */
    public String getFormFieldValueText(String fieldName){
        if (this.formFieldList != null){
            for (FormPropertyVo formPropertyVo : this.formFieldList) {
                if (fieldName.equals(formPropertyVo.getId())){
                    return formPropertyVo.getValueText();
                }
            }
        }
        return null;
    }

    /**
     * 添加定制表单地址
     * @param formName 表单名称
     * @param viewPath 表单地址
     * @param data 数据
     */
    public void addCustomForm(String formName,String viewPath, JSONObject data){
        if (this.customFormVoList == null){
            this.customFormVoList = new ArrayList<>();
        }

        this.customFormVoList.add(new CustomFormVo(formName,viewPath,data));
    }

    /**
     * 添加定制表单地址
     * @param name 表单名称
     * @param path 表单地址
     * @param initData 数据
     */
    public void addBusinessPathArray(String name,String path,JSONObject initData){
        if (this.businessPathArray == null){
            this.businessPathArray = new JSONArray();
        }
        JSONObject businessPath = new JSONObject();
        businessPath.put("name",name);
        businessPath.put("path",path);
        businessPath.put("initData",initData);
        businessPathArray.add(businessPath);

    }

    public String getTitle() {
        return title;
    }

    public String getDataId() {
        return dataId;
    }

    public void setDataId(String dataId) {
        this.dataId = dataId;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getApplyUserName() {
        return applyUserName;
    }

    public void setApplyUserName(String applyUserName) {
        this.applyUserName = applyUserName;
    }

    public String getApplyNickNam() {
        return applyNickName;
    }

    public void setApplyNickNam(String applyNickNam) {
        this.applyNickName = applyNickNam;
    }


    public void setApplyTime(Date applyTime) {
        this.applyTime = applyTime;
    }



    public String getCurrent() {
        return current;
    }

    public void setCurrent(String current) {
        this.current = current;
    }

    public String getProcessName() {
        return processName;
    }

    public void setProcessName(String processName) {
        this.processName = processName;
    }

    public String getTaskId() {
        return taskId;
    }

    public void setTaskId(String taskId) {
        this.taskId = taskId;
    }

    public Date getApplyTime() {
        return applyTime;
    }

    public String getApplyUserId() {
        return applyUserId;
    }

    public void setApplyUserId(String applyUserId) {
        this.applyUserId = applyUserId;
    }

    public String getDataTable() {
        return dataTable;
    }

    public void setDataTable(String dataTable) {
        this.dataTable = dataTable;
    }

    public String getTaskKey() {
        return taskKey;
    }

    public void setTaskKey(String taskKey) {
        this.taskKey = taskKey;
    }

    public String getActId() {
        return actId;
    }

    public void setActId(String actId) {
        this.actId = actId;
    }

    public String getActFormPath() {
        return actFormPath;
    }

    public void setActFormPath(String actFormPath) {
        this.actFormPath = actFormPath;
    }

    public Map<String, Object> getProcessVariables() {
        return processVariables;
    }

    public void setProcessVariables(Map<String, Object> processVariables) {
        this.processVariables = processVariables;
    }



    public String getProcessDefKey() {
        return processDefKey;
    }

    public void setProcessDefKey(String processDefKey) {
        this.processDefKey = processDefKey;
    }

    public String getCompleteType() {
        return completeType;
    }

    public void setCompleteType(String completeType) {
        this.completeType = completeType;
    }

    public String getTaskName() {
        return taskName;
    }

    public void setTaskName(String taskName) {
        this.taskName = taskName;
    }

    public JSONArray getCcUser() {
        return ccUser;
    }

    public void setCcUser(JSONArray ccUser) {
        this.ccUser = ccUser;
    }

    public JSONArray getAddUser() {
        return addUser;
    }

    public void setAddUser(JSONArray addUser) {
        this.addUser = addUser;
    }

    public String getEntrustUser() {
        return entrustUser;
    }

    public void setEntrustUser(String entrustUser) {
        this.entrustUser = entrustUser;
    }

    public String getNextApproval() {
        return nextApproval;
    }

    public void setNextApproval(String nextApproval) {
        this.nextApproval = nextApproval;
    }

    public String getProcessDefId() {
        return processDefId;
    }

    public void setProcessDefId(String processDefId) {
        this.processDefId = processDefId;
    }

    public String getAssignee() {
        return assignee;
    }

    public void setAssignee(String assignee) {
        this.assignee = assignee;
    }

    public List<FormPropertyVo> getFormFieldList() {
        return formFieldList;
    }

    public void setFormFieldList(List<FormPropertyVo> formFieldList) {
        this.formFieldList = formFieldList;
    }

    public JSONArray getFileIdList() {
        return fileIdList;
    }

    public void setFileIdList(JSONArray fileIdList) {
        this.fileIdList = fileIdList;
    }

    public String getUrgent() {
        return urgent;
    }

    public void setUrgent(String urgent) {
        this.urgent = urgent;
    }

    public JSONArray getNextApprovalArray() {
        return nextApprovalArray;
    }

    public void setNextApprovalArray(JSONArray nextApprovalArray) {
        this.nextApprovalArray = nextApprovalArray;
    }

    public String getActModifyFrom() {
        return actModifyFrom;
    }

    public void setActModifyFrom(String actModifyFrom) {
        this.actModifyFrom = actModifyFrom;
    }

    public String getActViewPath() {
        return actViewPath;
    }

    public void setActViewPath(String actViewPath) {
        this.actViewPath = actViewPath;
    }

    public String getApplyNickName() {
        return applyNickName;
    }

    public void setApplyNickName(String applyNickName) {
        this.applyNickName = applyNickName;
    }

    public String getRejectedNode() {
        return rejectedNode;
    }

    public void setRejectedNode(String rejectedNode) {
        this.rejectedNode = rejectedNode;
    }

    public String getRejectedCompleteType() {
        return rejectedCompleteType;
    }

    public void setRejectedCompleteType(String rejectedCompleteType) {
        this.rejectedCompleteType = rejectedCompleteType;
    }

    public JSONArray getAssociatedProcess() {
        return associatedProcess;
    }

    public void setAssociatedProcess(JSONArray associatedProcess) {
        this.associatedProcess = associatedProcess;
    }

    public String getSelectApproval() {
        return selectApproval;
    }

    public void setSelectApproval(String selectApproval) {
        this.selectApproval = selectApproval;
    }


    public JSONArray getNextNodeApproval() {
        return nextNodeApproval;
    }

    public void setNextNodeApproval(JSONArray nextNodeApproval) {
        this.nextNodeApproval = nextNodeApproval;
    }

    public String getActFormId() {
        return actFormId;
    }

    public void setActFormId(String actFormId) {
        this.actFormId = actFormId;
    }

    public String getActApprovalRecord() {
        return actApprovalRecord;
    }

    public void setActApprovalRecord(String actApprovalRecord) {
        this.actApprovalRecord = actApprovalRecord;
    }

    public String getActApprovalPng() {
        return actApprovalPng;
    }

    public void setActApprovalPng(String actApprovalPng) {
        this.actApprovalPng = actApprovalPng;
    }

    public String getTaskType() {
        return taskType;
    }

    public void setTaskType(String taskType) {
        this.taskType = taskType;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public ViewRuTask getViewRuTask() {
        return viewRuTask;
    }

    public void setViewRuTask(ViewRuTask viewRuTask) {
        this.viewRuTask = viewRuTask;
    }

    public Task getTask() {
        return task;
    }

    public void setTask(Task task) {
        this.task = task;
    }

    public JSONArray getBusinessPathArray() {
        return businessPathArray;
    }

    public void setBusinessPathArray(JSONArray businessPathArray) {
        this.businessPathArray = businessPathArray;
    }

    public String getAddTaskRemark() {
        return addTaskRemark;
    }

    public void setAddTaskRemark(String addTaskRemark) {
        this.addTaskRemark = addTaskRemark;
    }

    public String getUploadfile() {
        return uploadfile;
    }

    public void setUploadfile(String uploadfile) {
        this.uploadfile = uploadfile;
    }

    public String getAddTaskModifyFrom() {
        return addTaskModifyFrom;
    }

    public void setAddTaskModifyFrom(String addTaskModifyFrom) {
        this.addTaskModifyFrom = addTaskModifyFrom;
    }

    public String getNewTaskName() {
        return newTaskName;
    }

    public void setNewTaskName(String newTaskName) {
        this.newTaskName = newTaskName;
    }

    public List<CustomFormVo> getCustomFormVoList() {
        return customFormVoList;
    }

    public void setCustomFormVoList(List<CustomFormVo> customFormVoList) {
        this.customFormVoList = customFormVoList;
    }

    public String getSupplementaryComments() {
        return supplementaryComments;
    }

    public void setSupplementaryComments(String supplementaryComments) {
        this.supplementaryComments = supplementaryComments;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getSupplementaryCommentsId() {
        return supplementaryCommentsId;
    }

    public void setSupplementaryCommentsId(String supplementaryCommentsId) {
        this.supplementaryCommentsId = supplementaryCommentsId;
    }

    public boolean getJumpEntrust() {
        return jumpEntrust;
    }

    public void setJumpEntrust(boolean jumpEntrust) {
        this.jumpEntrust = jumpEntrust;
    }

    public List<FileBase> getFileList() {
        return fileList;
    }

    public void setFileList(List<FileBase> fileList) {
        this.fileList = fileList;
    }

    public String getActTaskDocumentation() {
        return actTaskDocumentation;
    }

    public void setActTaskDocumentation(String actTaskDocumentation) {
        this.actTaskDocumentation = actTaskDocumentation;
    }
}
